---简单的工具函数，封装了一些效果，供菜鸟使用
local Utility = {}


--视为使用一张牌
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要使用牌的玩家
---@param card string|string[] @ 要使用的牌名，可以是一种或是多种
---@param skillname string @ 技能名
Utility.useVirtualCard = function(room, player, mingzi,skillname)
    room:askToUseVirtualCard(player,{
        name = mingzi,
        skill_name = skillname or "game_rule",
        cancelable = true,
        prompt = "#cainiao-virtualuse",
      })
end

--令一名玩家移动场上一张牌
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要移动牌的玩家
Utility.moveCardInBoard = function(room, player)
    if #room:canMoveCardInBoard() > 0 then
        local targets = room:askToChooseToMoveCardInBoard(player, {
          skill_name = "game_rule",
          prompt = "#cainiao_utility-move",
        cancelable = true,
          })
          if #targets > 0 then
            room:askToMoveCardInBoard(player, {
              target_one = targets[1],
              target_two = targets[2],
              skill_name = "game_rule",
            })
          end
    end
end

--令一名玩家弃置若干牌，不足则全弃置
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要弃置牌的玩家
---@param min_num integer @ 最小数量
---@param max_num integer @ 最大数量,不填就是最小数量
Utility.discardCard = function(room, player,  min_num, max_num)
    max_num = max_num or min_num
    room:askToDiscard(player, {
        min_num = min_num,
        max_num = max_num,
        include_equip = true,
        skill_name = "game_rule",
        cancelable = false,
      })
end

--令一名玩家回复体力
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要回复体力的玩家
---@param num integer @ 回复的体力值
Utility.recover = function(room, player, num)
    room:recover({ who = player, num = num, skillName = "game_rule" })
end

--一名玩家对另一名玩家造成伤害
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要造成伤害的玩家
---@param target ServerPlayer @ 被伤害的玩家
---@param num integer @ 伤害值
---@param damagetype integer @ 伤害类型,默认是普通伤害
Utility.damage = function(room, player, target, num, damagetype)
    damagetype = damagetype or fk.NormalDamage
    room:damage({ 
      to = target, 
      damage = num, 
      skillName = "game_rule",
      damageType = damagetype,
  })
end

--玩家进行从选项中选择
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要进行选择的玩家
---@param choices string[] @ 选择项
---@return string @ 选择项
Utility.choose = function(room, player, choices)
    return room:askToChoice(player, {
      choices = choices,
      skill_name = "game_rule",
      prompt = "#cainiao_utility-choose",
    })
end


---玩家选择一名或几名角色,默认范围是所有角色,
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要选择角色的玩家
---@param min_num integer @ 最小数量
---@param max_num integer @ 最大数量
---@param targets ServerPlayer[] @ 可选角色
---@return ServerPlayer[] @ 选择项
Utility.choosePlayer = function(room, player, min_num, max_num, targets,prompt)
    min_num = min_num or 1
    if max_num == 0 or max_num == nil then
        max_num = min_num
    end
    if not targets then
        targets = room.alive_players
    end
    prompt = prompt or "#cainiao_utility-chooseplayer"
    local tar=room:askToChoosePlayers(player, {
      min_num = min_num,
      max_num = max_num,
      targets = targets,
      skill_name = "game_rule",
      prompt = prompt,
    })

    return tar
end



----玩家打开选卡界面选卡，默认选手牌
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要选卡的玩家
---@param min_num integer @ 最小数量
---@param max_num integer @ 最大数量
---@param cards string[] @ 可选牌
---@param skillname string @ 技能名
Utility.chooseCards = function(room, player, min_num, max_num, cards, skillname)
  local filter
  if not cards then
    filter="h"
  else
    filter = { card_data = {{ player.general, cards }} }
  end
  local cards = room:askToChooseCards(player, {
    target = player,
    min = min_num,
    max = max_num,
    flag = filter,
    skill_name = skillname or "game_rule",
    prompt = "#cainiao_utility-choosecard",
  })
  return cards
end

------------复合效果


---玩家选择若干角色，并对他们造成伤害
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要造成伤害的玩家
---@param min_num integer @ 最小数量
---@param max_num integer @ 最大数量
---@param targets ServerPlayer[] @ 可选角色
---@param num integer @ 伤害值
---@param damagetype integer @ 伤害类型,默认是普通伤害
Utility.damagetoPlayer = function(room, player, min_num, max_num, targets, num, damagetype)
  local tar=Utility.choosePlayer(room, player, min_num, max_num, targets)
  for _, t in ipairs(tar) do
    Utility.damage(room, player, t, num, damagetype)
  end

end








---为技能添加技能的穿甲效果，这样造成的伤害可以无视护甲了
--用法Utility.Ignoreshieldeffect(skill)就行了
Utility.Ignoreshieldeffect =function (skill)
    skill:addEffect(fk.BeforeHpChanged, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and data.damageEvent and data.damageEvent.from == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x=data.shield_lost or 0
    if x>0 then
      data.num = data.num - data.shield_lost
      data.shield_lost=0
    end
  end,
})
end

---获取本回合进入弃牌堆的牌（中央区），返回牌的id表
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要获取弃牌堆的玩家
---@return string[] @ 弃牌堆的牌
Utility.getcantercard = function(room, player)
  local all_cards = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(all_cards, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  return all_cards
end


--弃置一名角色若干张牌
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要弃置牌的玩家
---@param target ServerPlayer @ 要被弃置牌的角色
---@param min_num integer @ 弃置的牌数
---@param max_num integer @ 弃置的牌数
Utility.discardtargetcards = function(room, player, target, min_num, max_num)
  local cards = room:askToChooseCards(player, {
    min = min_num,
    max = max_num,
    target = target,
    flag = "he",
    skill_name = "game_rule",
  })
  room:throwCard(cards, "game_rule", target, player)
  return cards
end

--一名玩家选择并弃置一名角色若干张牌（过河拆桥）
---@param room Room @ 所属的房间
---@param player ServerPlayer @ 要弃置牌的玩家
---@param target ServerPlayer @ 要被弃置牌的角色
---@param min_num integer @ 弃置的牌数
---@param max_num integer @ 弃置的牌数
---@param prompt string @ 提示
---@param targets ServerPlayer[] @ 可选角色
Utility.discardplayercards = function(room, player, min_num, max_num,prompt,targets)
  local tar=Utility.choosePlayer(room, player, 1, 1,targets,prompt)
  local cards=Utility.discardtargetcards(room, player, tar[1], min_num, max_num)
  return cards
end

   

Fk:loadTranslationTable{
    ["#cainiao-virtualuse"] = "请视为使用一张牌",
    ["#cainiao_utility-move"] = "请移动一张牌",
    ["#cainiao_utility-choose"] = "请选择一项",
    ["#cainiao_utility-chooseplayer"] = "请选择目标",
    ["#cainiao_utility-choosecard"] = "请选择牌",
}


return Utility